package com.kly.cashmall.module.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kly.cashmall.R;
import com.kly.cashmall.base.BaseBarFragment;
import com.kly.cashmall.base.app_action.BaseActionHelper;
import com.kly.cashmall.bean.MineInfoBean;
import com.kly.cashmall.framework.mvp.PresenterLifeCycle;
import com.kly.cashmall.module.mine.adapter.AccountListAdapter;
import com.kly.cashmall.module.mine.presenter.PersonalPresenter;
import com.kly.cashmall.module.mine.presenter.PersonalViewer;
import com.kly.cashmall.popup.TakePhotoWindow;
import com.kly.cashmall.utils.function.GlideUtils;
import com.kly.cashmall.utils.data.TextUtil;
import com.kly.cashmall.utils.aliyun.FileOssUpload;
import com.kly.cashmall.widget.picker.PhotoPicker;
import com.kly.cashmall.widget.picker.TakeListener;


/**
 * Created by
 * on 2019/5/5.
 */
public class AccountFragment extends BaseBarFragment implements PersonalViewer {

    private static final String TAG = AccountFragment.class.getSimpleName();
    @PresenterLifeCycle
    PersonalPresenter presenter = new PersonalPresenter(this);
    private AccountListAdapter mAdapter;
    private RecyclerView mAccountList;
    private PhotoPicker mPhotoPicker;
    private TakePhotoWindow popupWindow;
    private String realName;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_personal;
    }

    @Override
    protected void setView(@Nullable Bundle savedInstanceState) {
        setActionBar(false);

        mAccountList = bindView(R.id.account_list);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        mAccountList.setLayoutManager(manager);
        mAdapter = new AccountListAdapter();
        mAccountList.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            BaseActionHelper.with(getContext()).handleAction(mAdapter.getItem(position).getJumpUrl());
        });

        mPhotoPicker = PhotoPicker.create(this);
        mPhotoPicker.setTakeListener(new TakeListener() {
            @Override
            public void onTakeSuccess(String path) {
                GlideUtils.loadImage(getActivity(),path,bindView(R.id.person_avatar));
                FileOssUpload.uploadFile(getActivity(), path, new FileOssUpload.UploadCallback<String>() {
                    @Override
                    public void success(String s) {
                        presenter.saveUserHead(s, realName);

                    }

                    @Override
                    public void fail(String msg) {

                    }
                });
            }

            @Override
            public void onTakeFailure(String s) {

            }
        });
        bindView(R.id.person_avatar, v -> {
            popupWindow = new TakePhotoWindow(getActivity());
            popupWindow.setTakeListener(new TakePhotoWindow.OnTakeListener() {
                @Override
                public void openCamera() {
                    mPhotoPicker.pickFromCamera();
                }

                @Override
                public void openGallery() {
                    mPhotoPicker.pickFromGallery();
                }
            });
            popupWindow.setPopupGravity(Gravity.CENTER);
            popupWindow.showPopupWindow();
        });
    }

    @Override
    protected void loadData() {
        presenter.getUserInfo();
    }

    @Override
    public void getUserInfoSuccess(MineInfoBean infoBean) {
        bindView(R.id.head_container, true);
        bindView(R.id.tv_tools, true);
        realName = infoBean.getRealName();
        bindText(R.id.person_realname, realName);
        if (!TextUtil.isEmpty(infoBean.getAvatar())) {
           GlideUtils.loadImage(getActivity(),infoBean.getAvatar(),R.mipmap.ic_head_log,bindView(R.id.person_avatar));
        }
        bindView(R.id.loan_container,view->{
            BaseActionHelper.with(getContext()).handleAction(infoBean.getLoanOrderList().get(0).getJumpUrl());
        });
        bindView(R.id.profile_container,view->{
            BaseActionHelper.with(getContext()).handleAction(infoBean.getLoanOrderList().get(1).getJumpUrl());
        });

        mAdapter.setList(infoBean.getToolsList());
    }

    @Override
    public void setUserPhoto(String path) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mPhotoPicker != null) {
            mPhotoPicker.handleActivityResult(requestCode, resultCode, data);
        }
    }
}
